// 卡券管理路由

const {
  getCoupons,
  createCoupon,
  updateCouponStatus,
  getCouponDetail,
  updateCoupon,
  claimCoupon,
  getUserCoupons,
  useCoupon
} = require('../controllers/coupon');

async function coupons(fastify, options) {
  // 获取卡券列表
  fastify.get('/coupons', {
    schema: {
      description: '获取卡券列表',
      tags: ['卡券管理'],
      summary: '获取所有卡券信息',
      querystring: {
        type: 'object',
        properties: {
          type: { type: 'string', description: '卡券类型：储值卡/期限卡/优惠券/体验券' },
          status: { type: 'string', description: '卡券状态：上架/下架' },
          shopId: { type: 'string', description: '店铺ID' },
          page: { type: 'number', description: '页码' },
          pageSize: { type: 'number', description: '每页数量' }
        }
      },
      response: {
        200: {
          description: '成功获取卡券列表',
          type: 'object',
          properties: {
            code: { type: 'number', description: '状态码' },
            data: {
              type: 'object',
              properties: {
                total: { type: 'number', description: '总数' },
                list: {
                  type: 'array',
                  items: {
                    type: 'object',
                    properties: {
                      id: { type: 'string', description: '卡券ID' },
                      name: { type: 'string', description: '卡券名称' },
                      type: { type: 'string', description: '卡券类型' },
                      value: { type: 'number', description: '卡券面值' },
                      status: { type: 'string', description: '卡券状态' },
                      shopId: { type: 'string', description: '店铺ID' },
                      shopName: { type: 'string', description: '店铺名称' },
                      validStartTime: { type: 'string', description: '有效期开始时间' },
                      validEndTime: { type: 'string', description: '有效期结束时间' },
                      minConsume: { type: 'number', description: '最低消费金额' },
                      stock: { type: 'number', description: '库存数量' },
                      isPaid: { type: 'boolean', description: '是否付费购买' },
                      price: { type: 'number', description: '购买价格' },
                      isOneTime: { type: 'boolean', description: '是否仅限领取一次' },
                      createTime: { type: 'string', description: '创建时间' }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    handler: getCoupons
  });

  // 创建卡券
  fastify.post('/coupons', {
    schema: {
      description: '创建新卡券',
      tags: ['卡券管理'],
      summary: '创建新的卡券',
      body: {
        type: 'object',
        required: ['name', 'type', 'value', 'validStartTime', 'validEndTime'],
        properties: {
          name: { type: 'string', description: '卡券名称' },
          type: { type: 'string', description: '卡券类型：储值卡/期限卡/优惠券/体验券' },
          value: { type: 'number', description: '卡券面值' },
          shopId: { type: 'string', description: '店铺ID，为空表示全店铺通用' },
          validStartTime: { type: 'string', description: '有效期开始时间' },
          validEndTime: { type: 'string', description: '有效期结束时间' },
          minConsume: { type: 'number', description: '最低消费金额' },
          stock: { type: 'number', description: '库存数量' },
          isPaid: { type: 'boolean', description: '是否付费购买' },
          price: { type: 'number', description: '购买价格' },
          isOneTime: { type: 'boolean', description: '是否仅限领取一次' },
          description: { type: 'string', description: '卡券描述' },
          instruction: { type: 'string', description: '卡券使用说明（富文本）' },
          seatTypes: { 
            type: 'array',
            description: '限制使用的座位类型列表',
            items: { type: 'string' }
          }
        }
      },
      response: {
        200: {
          description: '成功创建卡券',
          type: 'object',
          properties: {
            code: { type: 'number', description: '状态码' },
            message: { type: 'string', description: '成功信息' }
          }
        }
      }
    },
    handler: createCoupon
  });

  // 更新卡券状态
  fastify.put('/coupons/:id/status', {
    schema: {
      description: '更新卡券状态',
      tags: ['卡券管理'],
      summary: '上架或下架卡券',
      params: {
        type: 'object',
        properties: {
          id: { type: 'string', description: '卡券ID' }
        }
      },
      body: {
        type: 'object',
        required: ['status'],
        properties: {
          status: { type: 'string', description: '卡券状态：上架/下架' }
        }
      },
      response: {
        200: {
          description: '成功更新卡券状态',
          type: 'object',
          properties: {
            code: { type: 'number', description: '状态码' },
            message: { type: 'string', description: '成功信息' }
          }
        }
      }
    },
    handler: updateCouponStatus
  });

  // 获取单个卡券详情
  fastify.get('/coupons/:id', {
    schema: {
      description: '获取单个卡券详情',
      tags: ['卡券管理'],
      summary: '根据ID获取卡券详细信息',
      params: {
        type: 'object',
        properties: {
          id: { type: 'string', description: '卡券ID' }
        }
      },
      response: {
        200: {
          description: '成功获取卡券详情',
          type: 'object',
          properties: {
            code: { type: 'number', description: '状态码' },
            data: {
              type: 'object',
              properties: {
                id: { type: 'string', description: '卡券ID' },
                name: { type: 'string', description: '卡券名称' },
                type: { type: 'string', description: '卡券类型' },
                value: { type: 'number', description: '卡券面值' },
                status: { type: 'string', description: '卡券状态' },
                shopId: { type: 'string', description: '店铺ID' },
                shopName: { type: 'string', description: '店铺名称' },
                validStartTime: { type: 'string', description: '有效期开始时间' },
                validEndTime: { type: 'string', description: '有效期结束时间' },
                minConsume: { type: 'number', description: '最低消费金额' },
                stock: { type: 'number', description: '库存数量' },
                isPaid: { type: 'boolean', description: '是否付费购买' },
                price: { type: 'number', description: '购买价格' },
                isOneTime: { type: 'boolean', description: '是否仅限领取一次' },
                description: { type: 'string', description: '卡券描述' },
                instruction: { type: 'string', description: '卡券使用说明（富文本）' },
                seatTypes: { 
                  type: 'array',
                  description: '限制使用的座位类型列表',
                  items: { type: 'string' }
                },
                createTime: { type: 'string', description: '创建时间' }
              }
            }
          }
        }
      }
    },
    handler: getCouponDetail
  });

  // 更新卡券信息
  fastify.put('/coupons/:id', {
    schema: {
      description: '更新卡券信息',
      tags: ['卡券管理'],
      summary: '更新卡券的详细信息',
      params: {
        type: 'object',
        properties: {
          id: { type: 'string', description: '卡券ID' }
        }
      },
      body: {
        type: 'object',
        properties: {
          name: { type: 'string', description: '卡券名称' },
          type: { type: 'string', description: '卡券类型' },
          value: { type: 'number', format: 'float', description: '卡券面值' },
          storeId: { type: ['string', 'null'], description: '店铺ID，null表示全店铺通用' },
          validStartTime: { type: 'string', format: 'date-time', description: '有效期开始时间' },
          validEndTime: { type: 'string', format: 'date-time', description: '有效期结束时间' },
          minConsume: { type: ['number', 'null'], format: 'float', description: '最低消费金额' },
          stock: { type: 'integer', description: '库存数量' },
          isPaid: { type: 'boolean', description: '是否付费购买' },
          price: { type: 'number', format: 'float', description: '购买价格' },
          isOneTime: { type: 'boolean', description: '是否仅限领取一次' },
          description: { type: 'string', description: '卡券描述' },
          instruction: { type: 'string', description: '卡券使用说明（富文本）' },
          status: { 
            type: 'string', 
            enum: ['上架', '下架'],
            description: '卡券状态'
          },
          isActive: { 
            type: 'boolean', 
            description: '是否激活'
          },
          seatTypes: { 
            type: 'array',
            description: '限制使用的座位类型列表',
            items: { type: 'string' }
          }
        }
      },
      response: {
        200: {
          description: '成功更新卡券信息',
          type: 'object',
          properties: {
            code: { type: 'number', description: '状态码' },
            message: { type: 'string', description: '成功信息' }
          }
        }
      }
    },
    handler: updateCoupon
  });

  // 用户领取卡券
  fastify.post('/coupons/claim', {
    schema: {
      description: '用户领取卡券',
      tags: ['用户卡券'],
      summary: '用户领取或购买卡券',
      body: {
        type: 'object',
        required: ['voucherId'],
        properties: {
          voucherId: { type: 'string', description: '卡券ID' }
        }
      },
      response: {
        200: {
          description: '成功领取卡券',
          type: 'object',
          properties: {
            code: { type: 'number', description: '状态码' },
            message: { type: 'string', description: '成功信息' },
            data: {
              type: 'object',
              properties: {
                issuedRecordId: { type: 'number', description: '发放记录ID' },
                voucherName: { type: 'string', description: '卡券名称' },
                voucherValue: { type: 'number', description: '卡券面值' },
                validStartTime: { type: 'string', description: '有效期开始时间' },
                validEndTime: { type: 'string', description: '有效期结束时间' }
              }
            }
          }
        },
        400: {
          description: '领取失败',
          type: 'object',
          properties: {
            code: { type: 'number', description: '状态码' },
            message: { type: 'string', description: '错误信息' }
          }
        }
      }
    },
    preHandler: [fastify.authenticate],
    handler: claimCoupon
  });

  // 获取用户已领取的卡券列表
  fastify.get('/user/coupons', {
    schema: {
      description: '获取用户已领取的卡券列表',
      tags: ['用户卡券'],
      summary: '查看用户已领取的所有卡券',
      querystring: {
        type: 'object',
        properties: {
          status: { type: 'string', description: '卡券状态：有效/已过期/未开始' },
          page: { type: 'number', description: '页码' },
          pageSize: { type: 'number', description: '每页数量' }
        }
      },
      response: {
        200: {
          description: '成功获取用户卡券列表',
          type: 'object',
          properties: {
            code: { type: 'number', description: '状态码' },
            data: {
              type: 'object',
              properties: {
                total: { type: 'number', description: '总数' },
                list: {
                  type: 'array',
                  items: {
                    type: 'object',
                    properties: {
                      id: { type: 'number', description: '发放记录ID' },
                      voucherId: { type: 'number', description: '卡券ID' },
                      voucherName: { type: 'string', description: '卡券名称' },
                      voucherType: { type: 'string', description: '卡券类型' },
                      voucherValue: { type: 'number', description: '卡券面值' },
                      isPaid: { type: 'boolean', description: '是否付费购买' },
                      price: { type: 'number', description: '购买价格' },
                      validStartTime: { type: 'string', description: '有效期开始时间' },
                      validEndTime: { type: 'string', description: '有效期结束时间' },
                      minConsume: { type: 'number', description: '最低消费金额' },
                      description: { type: 'string', description: '卡券描述' },
                      instruction: { type: 'string', description: '卡券使用说明' },
                      shopName: { type: 'string', description: '店铺名称' },
                      seatTypes: { 
                        type: 'array',
                        description: '限制使用的座位类型列表',
                        items: { type: 'string' }
                      },
                      claimTime: { type: 'string', description: '领取时间' },
                      status: { type: 'string', description: '卡券状态' }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    preHandler: [fastify.authenticate],
    handler: getUserCoupons
  });

  // 使用卡券
  fastify.post('/coupons/use', {
    schema: {
      description: '使用卡券',
      tags: ['用户卡券'],
      summary: '用户使用已领取的卡券',
      body: {
        type: 'object',
        required: ['voucherId', 'amount'],
        properties: {
          voucherId: { type: 'string', description: '卡券ID' },
          orderId: { type: 'string', description: '订单ID（可选）' },
          amount: { type: 'number', description: '使用金额' }
        }
      },
      response: {
        200: {
          description: '成功使用卡券',
          type: 'object',
          properties: {
            code: { type: 'number', description: '状态码' },
            message: { type: 'string', description: '成功信息' },
            data: {
              type: 'object',
              properties: {
                usedRecordId: { type: 'number', description: '使用记录ID' },
                voucherName: { type: 'string', description: '卡券名称' },
                usedAmount: { type: 'number', description: '使用金额' },
                remainingValue: { type: 'number', description: '剩余面值' }
              }
            }
          }
        },
        400: {
          description: '使用失败',
          type: 'object',
          properties: {
            code: { type: 'number', description: '状态码' },
            message: { type: 'string', description: '错误信息' }
          }
        }
      }
    },
    preHandler: [fastify.authenticate],
    handler: useCoupon
  });
}

module.exports = coupons; 